<?php
namespace app\backend\controller;
use think\Db;
use think\Controller;
use \think\Validate;
class Banner extends Controller
{
	 public function banner_add()
    {
    	return view('banner/banner_add');
    }
   public function banner_list()
    {
    	$ret=db('banner')->select();
      if(count($ret)!=0)
      {    	
      	foreach ($ret as $val) {
          		if($val['banner_visible']==0)
          	  {
          	  	$val['banner_visible']='显示';
          	  	$data[]=$val;
       
          	  }
          	  else
          	  {
          	  	$val['banner_visible']='隐藏';
          	  	$data[]=$val;

          	  }
          	}
      }
      else
      {
        $data=[];
      }
    	
    	$this->assign('data',$data);
    	return view('banner/banner_list');
    }
    public function insert()
    {
    	$banner_img=input('post.imgSrc');
    	$banner_visible=input('is_hidden');
      $banner_name=input('post.banner_name');
      $banner_sort=input('post.banner_sort');;
      $banner_describe=$_POST['banner_describe'];

      $rule = [
                'banner_name'  => 'require',
                'banner_img'   => 'require',
                'banner_sort' => 'require',
                'banner_describe' => 'require'
                
            ];
      $msg = [
          'banner_name.require' => '轮播图名称不能为空！',   
          'banner_img.require'  => '轮播图图片不能为空！',
          'banner_sort.require'  => '轮播图排序不能为空！',
          'banner_describe.require'  => '轮播图描述不能为空！'
          
      ];
      $data=[
          'banner_name'=>$banner_name,
          'banner_img'=>$banner_img,
          'banner_visible'=>$banner_visible,
          'banner_describe'=>$banner_describe,
          'banner_sort'=>$banner_sort
      ];

      $validate = new Validate($rule, $msg);
      $result   = $validate->check($data);
      if (!$result ) {
            $this->error($validate->getError());
        }
      $ret=db('banner')->insert($data);
         
       if($ret==false)
         {
         
          $this->error("插入失败");
         }
          $this->success('插入成功','backend/banner/banner_list'); 
    }
     public function banner_edit()
    {
    	 $banner_id=input('get.banner_id');
        if($banner_id!="")
        {
          $result=db('banner')->where("banner_id={$banner_id}")->select();
        }
          $this->assign('result',$result); 
    	    return view('banner/banner_edit');
    }
    public function update()
    {
	    $banner_id=input('post.banner_id'); 
	    $banner_visible=$_POST['is_hidden'];
      $banner_name=input('post.banner_name');
      $banner_sort=input('post.banner_sort');
      $banner_describe=$_POST['banner_describe'];
      $banner_img=isset($_POST['imgSrc'])?$_POST['imgSrc']:'';
       if($banner_img!='')
        {        
            $updata=[
                'banner_visible'=>$banner_visible,
                'banner_name'=>$banner_name,
                'banner_describe'=>$banner_describe,
                'banner_img'=>$banner_img,
                'banner_sort'=>$banner_sort

            ];
         }
        else{
             $updata=[
              'banner_visible'=>$banner_visible,
              'banner_name'=>$banner_name,
              'banner_describe'=>$banner_describe,
              'banner_sort'=>$banner_sort
             
            ];}


       $ret=db('banner')->where("banner_id={$banner_id}")->update($updata);
         if($ret===0)
        {
          $this->error("没有任何修改");
        }
         if($ret===false)
        {
          $this->error("修改失败");
        }
          $this->success('修改成功','backend/banner/banner_list');
   
    }
    

     public function upimg()
    
    {
    	$file = request()->file('banner_img');
    if($file){
	        $info = $file->move(ROOT_PATH . 'public' . DS . 'banner');
	        if($info){
	            // 成功上传后 获取上传信息
	            // 输出 jpg
	            
	            // 输出 图片名称.jpg
	            $img_src="/banner/{$info->getSaveName()}"; 
	            echo $img_src;
	          
			        }else{
			            // 上传失败获取错误信息
			           $this->error($file->getError());
			        }
	       }
    } 

    public function banner_delete()
    
    {
      $banner_id=input('get.banner_id');
      if($banner_id!="")
      {
        $ret=db('banner')->where("banner_id={$banner_id}")->delete();
        if($ret==false)
        {
          $this->error('删除失败');
        }
          $this->success('删除成功');

      }
    } 
}